﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FCTC_Web.Models;
using DAL;

namespace FCTC_Web.Utilities
{
    public class UploadHelper
    {
        public bool CheckImgType(string strType)
        {
            switch (strType)
            {
                case ("image/gif"):
                    return true;
                case ("image/jpg"):
                    return true;
                case ("image/jpeg"):
                    return true;
                case ("image/pjpeg"):
                    return true;
                case ("application/x-shockwave-flash"):
                    return true;
                default:
                    return false;
            }
        }
        public static ImageModel UploadImg(HttpRequestBase request, ImageModel img = null)
        {
            if (img == null) img = new ImageModel();

            var file = request.Files["file"];
            if (!string.IsNullOrWhiteSpace(file.FileName))
            {
                byte[] buf = new byte[file.ContentLength];

                file.InputStream.Read(buf, 0, file.ContentLength);
                img.ImageData = buf;
                img.ContentType = file.ContentType;
            }
            img.UpdateTime = DateTime.Now;

            if (img.ID > 0)
            {
                if (img.ImageData == null)
                {
                    var o = CH.GetDataById<ImageModel>(img.ID);
                    img.ImageData = o.ImageData;
                    img.ContentType = o.ContentType;
                }
                CH.Edit<ImageModel>(img);
            }
            else
                CH.Create<ImageModel>(img);
            return img;
        }

    }
}